import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))

from DataStructure.single_link_list import SingleLinkList


# 不创建新节点的情况下完成链表的分割, 但会变改原链表的数据
def single_link_list_split(la, lb, lc):
    tmp = la.head
    tmp_lb = lb
    tmp_lc = lc
    while tmp is not None:
        if lb.length() < 1:
            # tmp_lb[f"_{tmp_lb.__class__.__name__}__head"] = tmp
            tmp_lb.head = tmp
        else:
            tmp_lb.next = tmp
        tmp_lb = tmp
        tmp = tmp.next
        if tmp:
            if lc.length() < 1:
                # tmp_lc[f"_{tmp_lc.__class__.__name__}__head"] = tmp
                tmp_lc.head = tmp
            else:
                tmp_lc.next = tmp
            tmp_lc = tmp
            tmp = tmp.next

    tmp_lb.next = tmp_lc.next = None


# 创新新的节点完成链表的分割, 不影响原链表的数据
def single_link_list_split1(la, lb, lc):
    la = la.head
    while la is not None:
        lb.append(la.item)
        la = la.next
        if la is not None:
            lc.append(la.item)
            la = la.next


if __name__ == "__main__":
    la = SingleLinkList()
    for i in ["a", "b", "c", "d", "e", "f", "g", 1]:
        la.append(i)
    # print(la.head)
    # 把la单链表拆分成分别为lb单链表、lc单链表, lb中的每个元素为la中元素下标为奇数的元素, lc中的每个元素为la中元素下标为偶数的元素
    lb = SingleLinkList()
    lc = SingleLinkList()
    # single_link_list_split(la, lb, lc)
    single_link_list_split1(la, lb, lc)
    la.travel()
    lb.travel()
    lc.travel()

